#include <iostream>
#include <algorithm>
using namespace std;
int dp[111111];
int w[11111];
int v[11111];
int main()
{
    int t, m;
    cin >> t >> m;
    for (int i = 1; i <= m; i++)
        cin >> v[i] >> w[i];
    for (int i = 1; i <= m; i++)
    {
        for (int j = v[i]; j <= t; j++)
            dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
    }
    cout << dp[t] << endl;
    return 0;
}